function clean() {
    $('#usersDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#topicTitleField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#topicMessageField').parent().removeClass('has-error has-feedback').find('span').remove();
}

function validate() {
    clean();
    var validator = {valid: false};
    if ($("#usersDropDown").val() === "-1") {
        $('#usersDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid user!</span>').insertAfter('#usersDropDown');
        return validator;
    }

    var topicTitleField = $('#topicTitleField').val();
    if (!topicTitleField) {
        $('#topicTitleField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#topicTitleField');
        return validator;
    }
    if (topicTitleField.length > 100) {
        $('#topicTitleField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#topicTitleField');
        return validator;
    }

    var topicMessageField = $('#topicMessageField').val();
    if (!topicMessageField) {
        $('#topicMessageField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#topicMessageField');
        return validator;
    }
    if (topicMessageField.length > 700) {
        $('#topicMessageField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#topicMessageField');
        return validator;
    }

    validator["to_user_id"] = $("#usersDropDown").val();
    validator["topicTitleField"] = topicTitleField;
    validator["topicMessageField"] = topicMessageField;
    validator["valid"] = true;
    return validator;
}


function addNewEmailClick(eventObject) {
    var validator = validate();
    if (validator["valid"]) {
        sendNewEmail(validator["to_user_id"], validator["topicTitleField"], validator["topicMessageField"], function (data) {
            if (data["success"] !== -1) {
                $("#emailsSucessArea").append(
                        '<div class="alert alert-success alert-dismissible" role="alert">' +
                        '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                        '<strong>Well done!</strong>  You successfully sent the email!' + 
                        '</div>');
            } else {
                $("#emailsSucessArea").append(
                        '<div class="alert alert-danger alert-dismissible" role="alert">' +
                        '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                        '<strong>Well done!</strong>  Error sending the email!' + 
                        '</div>');
            }
        });
    }
}

$(document).ready(function () {
    getAllUsers(function (arrayUsers) {
        for (var i in arrayUsers) {
            $("#usersDropDown").append('<option value="' + arrayUsers[i].id_user + '">' + arrayUsers[i].name + '</option>');
        }
    });
});


